package com.zpark.controller;

import com.zpark.pojo.Empty;
import com.zpark.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;
//进行增删改查时，都要在user之下
/*
1,添加一个新查询功能时要先在数据访问对象，再service调用，最后再controller调用，
 */
/**
 * @PackageName: com.zpark.controller
 * @ClassName: UserController
 * @Description:
 * @author: RZS
 * @date: 2022/9/8  16:44
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/find_all_empty")
    public List<Empty> findAllEmpty(){
        return userService.findAllEmpty();
    }

    /**
     *  @RequestBody的作用，将请求参数以json格式接收
     */
    @RequestMapping("/add")
    public String addEmpty(Empty empty){
        userService.addEmpty(empty);
        return "添加用户成功";
    }

    @RequestMapping("ange")//访问时输入蓝色地址，增删改查都得在user下进行，即user/
    public String changeEmpty(Empty empty){
        userService.changeEmptyById(empty);
        return "修改用户成功";
        //修改用户时，要根据id来修改，所以在浏览器输入时，要加入id=4/...
    }

    // @PathVariable:获取url中的数据
    @RequestMapping("/del/{id}")//删除时只需要在user/del后加id=？即可
    public String removeEmpty(@PathVariable("id") Integer id){
        userService.removeEmptyById(id);
        return "删除用户成功";
    }

    //胡亚东的代码
    @RequestMapping("/find_by_user/{username}")
    public List<Empty> findByUsername(@PathVariable("username") String username){
        return userService.findByUsername(username);
    }

}
